package com.itcast.tanhuaserver.controller;

import com.itcast.tanhuaserver.service.VideoMQService;
import com.itcast.tanhuaserver.service.VideoService;
import com.itcast.tanhuaserver.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @PachageName: com.itcast.tanhuaserver.controller
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/1820:24
 * @Version 1.0
 */
@RestController
@RequestMapping("smallVideos")
@Slf4j
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private MovementsController movementsController;

    @Autowired
    private CommentsController commentsController;

    @Autowired
    private VideoMQService videoMQService;
    /**
     * 保存小视频
     * @param videoThumbnail
     * @param videoFile
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(@RequestParam(value = "videoThumbnail",required = false)MultipartFile videoThumbnail,@RequestParam(value = "videoFile",required = false)MultipartFile videoFile){
        try {
            String videoId = this.videoService.saveVideo(videoThumbnail,videoFile);
            if (StringUtils.isNotEmpty(videoId)){
                //发送消息
                this.videoMQService.videoMsg(videoId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("保存小视频失败~",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询小视频列表
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
        try {
            PageResult pageResult = this.videoService.queryVideoList(page,pageSize);
            if (pageResult != null){
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            log.error("查询小视频列表错误 ~",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.likeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()){
            this.videoMQService.likeVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 取消点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        ResponseEntity<Long> entity = this.movementsController.disLikeComment(videoId);
        if (entity.getStatusCode().is2xxSuccessful()){
            this.videoMQService.disLikeVideoMsg(videoId);
        }
        return entity;
        //return this.movementsController.disLikeComment(videoId);
    }

    /**
     * 评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryCommentsList(@PathVariable("id") String videoId,
                                                        @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {
        ResponseEntity<PageResult> entity = this.commentsController.queryCommentsList(videoId, page, pagesize);
        if (entity.getStatusCode().is2xxSuccessful()){
            this.videoMQService.commentVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 提交评论
     *
     * @param param
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> saveComments(@RequestBody Map<String, String> param,
                                             @PathVariable("id") String videoId) {
        param.put("movementId", videoId);
        ResponseEntity<Void> entity = this.commentsController.saveComments(param);
        if (entity.getStatusCode().is2xxSuccessful()){
            this.videoMQService.commentVideoMsg(videoId);
        }
        return entity;
    }

    /**
     * 评论点赞
     *
     * @param publishId
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Long> commentsLikeComment(@PathVariable("id") String publishId) {
        return this.movementsController.likeComment(publishId);
    }

    /**
     * 评论取消点赞
     *
     * @param publishId
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Long> disCommentsLikeComment(@PathVariable("id") String publishId) {
        return this.movementsController.disLikeComment(publishId);
    }

    /**
     * 视频用户关注
     * @return
     */
    @PostMapping("/{id}/userFocus")
    public ResponseEntity<Void> saveUserFocusComments(@PathVariable("id") Long userId){
        try {
            Boolean bool = this.videoService.saveUserFocusComments(userId);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("视频用户关注错误~ userId : " + userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消视频用户关注
     * @return
     */
    @PostMapping("/{id}/userUnFocus")
    public ResponseEntity<Void> saveUserUnFocusComments(@PathVariable("id") Long userId){
        try {
            Boolean bool = this.videoService.saveUserUnFocusComments(userId);
            if (bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("取消视频用户关注错误~ userId : " + userId,e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



}
